package com.hzlj.position.locate.controller;


import cn.hutool.core.util.ObjectUtil;
import com.fz.common.base.dto.DeleteCommonDTO;
import com.fz.common.base.dto.PageParamDTO;
import com.fz.common.base.dto.QueryPageResultDTO;
import com.fz.common.base.dto.ResultDTO;
import com.fz.common.base.exception.PubException;
import com.hzlj.position.locate.common.dto.reportInspectionAutoSetting.ReportInspectionAutoSettingAddDTO;
import com.hzlj.position.locate.common.dto.reportInspectionAutoSetting.ReportInspectionAutoSettingQueryParamDTO;
import com.hzlj.position.locate.common.dto.reportInspectionAutoSetting.ReportInspectionAutoSettingQueryResultDTO;
import com.hzlj.position.locate.common.dto.reportInspectionAutoSetting.ReportInspectionAutoSettingUpdateDTO;
import com.hzlj.position.config.common.enums.ReportType;
import com.hzlj.position.locate.service.ReportInspectionAutoSettingService;
import com.hzlj.position.locate.service.ReportInspectionService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * 【通信联络核查-核查-自动配置表】接口
 *
 * @author lifh
 * @date 2023-03-13 19:10:13
 */
@Slf4j
@RestController
@RequestMapping("/position/report/inspection/auto/setting")
public class ReportInspectionAutoSettingController {

    @Resource
    private ReportInspectionAutoSettingService reportInspectionAutoSettingService;
    @Resource
    private ReportInspectionService reportInspectionService;

    /**
     * 通过id查询通信联络核查-核查-自动配置表
     *
     * @param id id
     * @return 实例对象
     */
    @GetMapping("/{id}")
    public ResultDTO<ReportInspectionAutoSettingQueryResultDTO> getReportInspectionAutoSettingById(@PathVariable("id") String id) {
        return new ResultDTO<>(reportInspectionAutoSettingService.getReportInspectionAutoSettingById(id));
    }


    /**
     * 保存通信联络核查-核查-自动配置表
     *
     * @param dto 添加的信息
     * @return 实例对象
     */
    @PutMapping("")
    public ResultDTO<Boolean> saveReportInspectionAutoSetting(@RequestBody ReportInspectionAutoSettingAddDTO dto) {
        log.info("【新增信息】通信联络核查-核查-自动配置表={}", dto);
        this.checkJgCode(dto.getJgs(), dto.getCreatorJgCode());
        this.checkDay(dto.getStartDay(), dto.getEndDay());
        this.checkTime(dto.getStartTime(), dto.getEndTime());
        reportInspectionAutoSettingService.saveReportInspectionAutoSetting(dto);
        return new ResultDTO<>();
    }


    /**
     * 更新通信联络核查-核查-自动配置表
     *
     * @param dto 更新的信息
     * @return 是否更新成功
     */
    @PostMapping(value = "")
    public ResultDTO<Boolean> updateReportInspectionAutoSetting(@RequestBody ReportInspectionAutoSettingUpdateDTO dto) {
        log.info("【更新信息】通信联络核查-核查-自动配置表={}", dto);
        this.checkDay(dto.getStartDay(), dto.getEndDay());
        this.checkTime(dto.getStartTime(), dto.getEndTime());
        return new ResultDTO<>(reportInspectionAutoSettingService.updateReportInspectionAutoSetting(dto));
    }

    /**
     * 删除通信联络核查-核查-自动配置表
     * 1、已产生的点验任务（已下发 未  上报  记录）   删除
     * 2、已产生 点验任务 并已上报 （已下发  已上报）记录保留
     */
    @Transactional(rollbackFor = Exception.class)
    @DeleteMapping(value = "")
    public ResultDTO<Boolean> deleteReportInspectionAutoSetting(@RequestBody DeleteCommonDTO<String> dto) {
        log.info("【删除信息】通信联络核查-核查-自动配置表={}", dto);
        for (String id : dto.getAllIds()) {
            ReportInspectionAutoSettingQueryResultDTO setting = this.reportInspectionAutoSettingService.getReportInspectionAutoSettingById(id);
            reportInspectionService.deleteByJgCodeWithUnSuccess(ReportType.AUTO.getCode(), setting.getJgCode(), setting.getStartDay(), setting.getEndDay());
        }
        return new ResultDTO<>(reportInspectionAutoSettingService.deleteReportInspectionAutoSetting(dto));
    }

    /**
     * 分页查询 通信联络核查-核查-自动配置表
     *
     * @param dto          查询条件
     * @param pageParamDTO 分页信息
     * @return 对象列表
     */
    @GetMapping("/list/page")
    public ResultDTO<QueryPageResultDTO<ReportInspectionAutoSettingQueryResultDTO>> pageReportInspectionAutoSetting(ReportInspectionAutoSettingQueryParamDTO dto, PageParamDTO pageParamDTO) {
        return new ResultDTO<>(reportInspectionAutoSettingService.pageReportInspectionAutoSetting(dto, pageParamDTO));
    }

    private void checkJgCode(List<ReportInspectionAutoSettingAddDTO.Jg> jgs, String creatorJgCode) {
        if (ObjectUtil.isEmpty(jgs)) {
            throw new PubException("没有输入机构信息");
        }
        if (ObjectUtil.isEmpty(creatorJgCode)) {
            throw new PubException("没有创建者机构参数");
        }
    }

    private void checkDay(Date startDay, Date endDay) {
        //不能为空
        if (ObjectUtil.isEmpty(startDay) || ObjectUtil.isEmpty(endDay)) {
            throw new PubException("开始、结束日期不能为空");
        }
        //设置时间是否正确
        boolean startEmpty = ObjectUtil.isEmpty(startDay);
        boolean endEmpty = ObjectUtil.isEmpty(endDay);

        if ((startEmpty && !endEmpty) || (!startEmpty && endEmpty)) {
            throw new PubException("开始、结束日期只能同时同时不设置或同时设置");
        }

        if (!startEmpty && startDay.compareTo(endDay) > 0) {
            throw new PubException("开始日期不能大于结束日期");
        }
    }

    private void checkTime(String startTime, String endTime) {
        this.checkTime(startTime);
        this.checkTime(endTime);
        if (startTime.equals(endTime)) {
            throw new PubException("开始，结束时间不能相等");
        }
        if (endTime.compareTo(startTime) < 0) {
            throw new PubException("结束时间不能小于开始时间");
        }
    }

    private void checkTime(String time) {
        if (ObjectUtil.isEmpty(time)) {
            throw new PubException("必要参数不足, 时间必填");
        }
        if (time.length() != 5) {
            throw new PubException("时间点参数格式不正确");
        }
        if (time.equals("24:00")) {
            throw new PubException("不能设置24:00");
        }
    }

}

